<?php

/* 
 * 高德地图处理
 * @2015-12-16 wzl
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class mapController {
    protected $key = '73c85c3943862123e8966786906aeb6d';//高德KEY
    protected $city = '010';//暂只支持北京
    public static function load(){
        return new self();
    }
    /**
     * 判断地址是否支持配送
     * @param string $address 需要判断的地址
     * @return int 0不支持1支持
     */
    public function checkSupportDelivery($address) {
        $map = $this->getLocation($address);
        if($map) {
            $jd = $map['JD'];
            $wd = $map['WD'];
            $status = $this->checkJingWeiArea($jd, $wd);
            return $status;
        }
        return 0;
    }
    /**
     * 获取地址的经纬度
     * @param type $address
     * @return boolean
     */
    public function getLocation($address){        
        $url = 'http://restapi.amap.com/v3/geocode/geo?key=' . $this->key . '&s=rsv3&city=' . $this->city . '&address=' . urlencode($address);
        $json_str = api_data::load()->curl_vget($url);
        $map_arr = json_decode($json_str, true);
        if(isset($map_arr['geocodes'][0])) {
            $map_info = $map_arr['geocodes'][0];
            if(isset($map_info['location'])) {
                $location = $map_info['location'];
                $_map = explode(',', $location);
                $map = array(
                    'WD' => $_map[0],
                    'JD' => $_map[1],
                );
                return $map;
            }
        }
        return false;
    }
    /**
    * 判断一个经纬度是否在一个多边形内
    * @param $userjd 用户经度
    * @param $userwd 用户纬度
    * @param $areajw 多边形区域坐标
    * @return
    */
    public function checkJingWeiArea($userjd, $userwd)
    {
        $areajw = $this->getBjFiveArea();
        $intstate = 0;
        $intup = 0;
        $intbt = 0;
        $intlf = 0;
        $intrg = 0;
        $ALon = round($userjd,6);
        $ALat = round($userwd,6);
        $iSum = 0;
        for($i=0; $i< count($areajw) ; $i++)
        {
            $interr = 0;
            $dLon1 = round($areajw[$i]['JD'],6);
            $dLat1 = round($areajw[$i]['WD'],6);
            if($i == (count($areajw)-1))
            {
                $dLon2 = round($areajw[0]['JD'],6);
                $dLat2 = round($areajw[0]['WD'],6);
            }
            else
            {
                $dLon2 = round($areajw[($i+1)]['JD'],6);
                $dLat2 = round($areajw[($i+1)]['WD'],6);
            }
            if((($ALat >= $dLat1) && ($ALat < $dLat2)) || (($ALat >= $dLat2) && ($ALat < $dLat1)))
            {
                if(abs($dLat1-$dLat2) > 0)
                {
                    $dLon = $dLon1 - (($dLon1 - $dLon2) * ($dLat1 - $ALat)) / ($dLat1 - $dLat2);
                    if($dLon < $ALon)
                    {
                        $iSum++;
                    }
                }
            }
        }
        if (($iSum % 2) != 0 && $iSum > 0)
        {
            $intstate = 1;
        }
        return $intstate;
    }
    /**
     * 北京五环的多边形坐标。
     * @return array JD,WD
     */
    public function getBjFiveArea() {
        $list = array(
                    array('JD' => '40.023211', 'WD' => '116.353669'),
                    array('JD' => '40.045555', 'WD' => '116.336288'),
                    array('JD' => '40.051665', 'WD' => '116.365471'),
                    array('JD' => '40.056461', 'WD' => '116.386929'),
                    array('JD' => '40.06349', 'WD' => '116.385469'),
                    array('JD' => '40.065198', 'WD' => '116.381178'),
                    array('JD' => '40.067103', 'WD' => '116.381264'),
                    array('JD' => '40.067037', 'WD' => '116.383839'),
                    array('JD' => '40.099987', 'WD' => '116.383946'),
                    array('JD' => '40.10084', 'WD' => '116.384611'),
                    array('JD' => '40.102006', 'WD' => '116.384397'),
                    array('JD' => '40.099675', 'WD' => '116.364613'),
                    array('JD' => '40.098559', 'WD' => '116.335173'),
                    array('JD' => '40.098296', 'WD' => '116.314831'),
                    array('JD' => '40.095145', 'WD' => '116.29818'),
                    array('JD' => '40.061782', 'WD' => '116.323972'),
                    array('JD' => '40.059056', 'WD' => '116.317792'),
                    array('JD' => '40.056625', 'WD' => '116.304574'),
                    array('JD' => '40.054227', 'WD' => '116.297493'),
                    array('JD' => '40.051468', 'WD' => '116.284876'),
                    array('JD' => '40.047493', 'WD' => '116.258354'),
                    array('JD' => '40.04516', 'WD' => '116.252389'),
                    array('JD' => '40.038129', 'WD' => '116.26492'),
                    array('JD' => '40.030966', 'WD' => '116.265821'),
                    array('JD' => '40.020352', 'WD' => '116.267366'),
                    array('JD' => '40.011214', 'WD' => '116.26771'),
                    array('JD' => '40.007106', 'WD' => '116.270714'),
                    array('JD' => '40.005314', 'WD' => '116.269212'),
                    array('JD' => '40.003933', 'WD' => '116.266937'),
                    array('JD' => '40.003177', 'WD' => '116.263289'),
                    array('JD' => '40.00109', 'WD' => '116.245254'),
                    array('JD' => '40.00109', 'WD' => '116.245254'),
                    array('JD' => '39.998509', 'WD' => '116.23886'),
                    array('JD' => '39.997876', 'WD' => '116.236167'),
                    array('JD' => '39.996602', 'WD' => '116.228925'),
                    array('JD' => '39.995526', 'WD' => '116.225674'),
                    array('JD' => '39.994515', 'WD' => '116.224054'),
                    array('JD' => '39.992608', 'WD' => '116.222262'),
                    array('JD' => '39.988596', 'WD' => '116.221189'),
                    array('JD' => '39.950642', 'WD' => '116.215653'),
                    array('JD' => '39.950642', 'WD' => '116.215653'),
                    array('JD' => '39.948668', 'WD' => '116.214881'),
                    array('JD' => '39.944621', 'WD' => '116.212521'),
                    array('JD' => '39.940738', 'WD' => '116.211834'),
                    array('JD' => '39.92468', 'WD' => '116.21222'),
                    array('JD' => '39.913423', 'WD' => '116.21119'),
                    array('JD' => '39.904864', 'WD' => '116.21119'),
                    array('JD' => '39.891793', 'WD' => '116.211362'),
                    array('JD' => '39.885833', 'WD' => '116.20986'),
                    array('JD' => '39.878028', 'WD' => '116.207843'),
                    array('JD' => '39.874702', 'WD' => '116.208358'),
                    array('JD' => '39.870223', 'WD' => '116.210589'),
                    array('JD' => '39.859715', 'WD' => '116.218529'),
                    array('JD' => '39.857013', 'WD' => '116.220117'),
                    array('JD' => '39.853917', 'WD' => '116.220717'),
                    array('JD' => '39.848876', 'WD' => '116.222177'),
                    array('JD' => '39.845581', 'WD' => '116.224966'),
                    array('JD' => '39.838727', 'WD' => '116.231833'),
                    array('JD' => '39.833817', 'WD' => '116.23651'),
                    array('JD' => '39.832565', 'WD' => '116.239471'),
                    array('JD' => '39.831016', 'WD' => '116.244879'),
                    array('JD' => '39.828973', 'WD' => '116.248269'),
                    array('JD' => '39.815591', 'WD' => '116.259899'),
                    array('JD' => '39.815591', 'WD' => '116.259899'),
                    array('JD' => '39.811602', 'WD' => '116.263032'),
                    array('JD' => '39.80603', 'WD' => '116.265264'),
                    array('JD' => '39.79947', 'WD' => '116.265821'),
                    array('JD' => '39.796997', 'WD' => '116.266379'),
                    array('JD' => '39.780047', 'WD' => '116.272602'),
                    array('JD' => '39.776913', 'WD' => '116.27522'),
                    array('JD' => '39.774967', 'WD' => '116.278095'),
                    array('JD' => '39.773648', 'WD' => '116.283031'),
                    array('JD' => '39.774077', 'WD' => '116.288137'),
                    array('JD' => '39.775462', 'WD' => '116.292171'),
                    array('JD' => '39.780937', 'WD' => '116.302385'),
                    array('JD' => '39.781959', 'WD' => '116.307664'),
                    array('JD' => '39.781696', 'WD' => '116.310883'),
                    array('JD' => '39.779453', 'WD' => '116.330409'),
                    array('JD' => '39.779189', 'WD' => '116.335044'),
                    array('JD' => '39.777342', 'WD' => '116.366372'),
                    array('JD' => '39.775264', 'WD' => '116.371565'),
                    array('JD' => '39.775198', 'WD' => '116.371608'),
                    array('JD' => '39.770449', 'WD' => '116.374011'),
                    array('JD' => '39.761839', 'WD' => '116.376028'),
                    array('JD' => '39.758969', 'WD' => '116.378732'),
                    array('JD' => '39.758045', 'WD' => '116.383066'),
                    array('JD' => '39.758705', 'WD' => '116.386886'),
                    array('JD' => '39.758111', 'WD' => '116.383109'),
                    array('JD' => '39.763158', 'WD' => '116.405725'),
                    array('JD' => '39.766193', 'WD' => '116.416154'),
                    array('JD' => '39.769096', 'WD' => '116.420403'),
                    array('JD' => '39.772955', 'WD' => '116.423278'),
                    array('JD' => '39.782619', 'WD' => '116.427612'),
                    array('JD' => '39.786576', 'WD' => '116.431174'),
                    array('JD' => '39.788357', 'WD' => '116.433878'),
                    array('JD' => '39.790863', 'WD' => '116.442461'),
                    array('JD' => '39.79083', 'WD' => '116.455293'),
                    array('JD' => '39.791787', 'WD' => '116.460743'),
                    array('JD' => '39.793963', 'WD' => '116.464391'),
                    array('JD' => '39.805008', 'WD' => '116.475034'),
                    array('JD' => '39.811305', 'WD' => '116.482286'),
                    array('JD' => '39.819744', 'WD' => '116.504989'),
                    array('JD' => '39.822348', 'WD' => '116.511984'),
                    array('JD' => '39.825875', 'WD' => '116.51722'),
                    array('JD' => '39.844527', 'WD' => '116.542239'),
                    array('JD' => '39.851413', 'WD' => '116.549149'),
                    array('JD' => '39.85418', 'WD' => '116.549621'),
                    array('JD' => '39.865282', 'WD' => '116.548076'),
                    array('JD' => '39.879741', 'WD' => '116.548805'),
                    array('JD' => '39.89288', 'WD' => '116.547561'),
                    array('JD' => '39.913818', 'WD' => '116.543741'),
                    array('JD' => '39.937711', 'WD' => '116.542325'),
                    array('JD' => '39.942614', 'WD' => '116.540995'),
                    array('JD' => '39.944785', 'WD' => '116.539578'),
                    array('JD' => '39.950905', 'WD' => '116.533914'),
                    array('JD' => '39.958734', 'WD' => '116.529193'),
                    array('JD' => '39.968175', 'WD' => '116.524987'),
                    array('JD' => '39.976462', 'WD' => '116.519408'),
                    array('JD' => '39.996849', 'WD' => '116.502242'),
                    array('JD' => '40.012201', 'WD' => '116.486921'),
                    array('JD' => '40.015126', 'WD' => '116.478424'),
                    array('JD' => '40.015159', 'WD' => '116.462502'),
                    array('JD' => '40.015553', 'WD' => '116.458769'),
                    array('JD' => '40.021962', 'WD' => '116.435637'),
                    array('JD' => '40.022422', 'WD' => '116.408644'),
                    array('JD' => '40.023079', 'WD' => '116.353712'),
        );
        return $list;
    }
}


